
"""

	data store class for POI
	
"""

from google.appengine.ext import db
from google.appengine.ext.db import polymodel
from misc import ImageContainer
from journey import Journey
from regions import Region

import logging

log = logging.getLogger(__name__)

class WPType( db.Model ):
    
    name = db.StringProperty( required = True )
    desc = db.TextProperty()
    
    parent_type = db.SelfReferenceProperty( collection_name="subtypes" )
    
    @property
    def waypoints(self):
        return db.GqlQuery( "select * from WayPoint where type = :1", self ).fetch(1000)

class WayPoint( db.Model, ImageContainer ):
	
    """
		Waypoint
    """
    link		= db.ReferenceProperty()
	
	
    coords		= db.GeoPtProperty()
    arrived		= db.DateTimeProperty()
    leaved		= db.DateTimeProperty()
    desc        = db.TextProperty()
#	profile     = db.ReferenceProperty( Profile, collection_name="profile_waypoints" )
    journey		= db.ReferenceProperty( Journey, collection_name="journey_waypoints" )

    region		= db.ReferenceProperty( Region, collection_name="region_waypoints" )
    
    type		= db.ReferenceProperty( WPType, collection_name="type_waypoints" )

    pos         = db.IntegerProperty()
    
    #icon representing WayPoint on google map
    # 1 - default icon
    icon        = db.IntegerProperty(choices=[0,1])

    def safe_key(self):
            try:
                return self.key()
            except:
                return ""

    def lat(self):
        if( None == self.coords ):
            return None
        
        ll = str(self.coords).split(",")
	
        return ll[0].strip() 

    def lng(self):
        if( None == self.coords ):
            return None
        
        ll = str(self.coords).split(",")
	
        return ll[1].strip() 

    
            

